﻿Imports System.Windows.Forms
Imports System.Data.OleDb

Public Class LoadDialog

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        'Me.DialogResult = System.Windows.Forms.DialogResult.OK

        If txtLoad.Text = "" Then 'Dont allow black/empty QID Field
            MsgBox("QID Field Cannot be Empty", MsgBoxStyle.Exclamation) 'Tell user to be carefull at future
            Return 'Stop right there
        End If

        If Not IsNumeric(txtLoad.Text) Then 'If Textbox value isn't number format (numeric)
            MsgBox("Only Numbers Allowed", MsgBoxStyle.Exclamation) 'Notify User
            Return 'Stop immidiatelly
        End If

        LoadQuery(CInt(txtLoad.Text))


        Me.Close()


    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub LoadQuery(ByVal qid As Integer)

        Dim statement As String = "SELECT * FROM RecFinder_Log WHERE QID = " & qid 'Statement to retrieve rows that match the given QID
        Dim conn As New OleDbConnection(My.Settings.RecFinder_Logs) 'Connection String
        Dim query As String 'The Retrieved Query from the database
        Dim description As String 'the retrieved description from database
        If My.Settings.RegistryAccessible = True Then
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\Software\RecFinder\recfinder_connString", "path", Nothing) 'passing the path to logdb from registry
        Else
            conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Main.defaultPath

        End If


        Try
            Using adapter As New OleDbDataAdapter(statement, conn) 'use adapter for searching the database
                Dim dst As New DataSet 'the dataset for getting the data
                adapter.Fill(dst, "RecFinder_Log") 'fill dataset
                query = dst.Tables("RecFinder_Log").Rows(0).Item("UserQuery").ToString 'get the qyery that matched
                description = dst.Tables("RecFinder_Log").Rows(0).Item("Descr").ToString 'and so the description
                Main.desscBox.Enabled = True 'Activate MainForm Description Box

                Main.QueryBox.Text = query 'Main Form QueryBox gets the query value
                Main.desscBox.Text = description 'And so does the description box ...

                MsgBox("Query Loaded Successfully", MsgBoxStyle.Information) 'Notify User that everything worked fine

                qDbManager.Close() 'Close Form2 When Done

            End Using

        Catch ex As Exception 'Exception is more likely to occur in no match found so....
            MsgBox("No match Found", MsgBoxStyle.Critical) 'Notify user that no match found
            Return 'and stop there
        End Try

    End Sub
   
End Class
